var Defines = global.appdef;

module.exports = function(room) {
    return new GameLogic(room);
}

var GameLogic = function(room){
    this.gamePlayers = [];
    this.curidx = 0;
    this.rounds = 0;
    this.countDown = 60;
    this.cardId = 0;
    this.idGamePlayerMap = {};
    this.room = room;
}

GameLogic.prototype.StartGame = function(gamePlayers1, gamePlayers2){
    var GameCard = require('./model/gamecard');
    var DBTools = require('./tools/dbjson');

    this.gameStage = Defines.GameStage_Prepare
    this.gamePlayers = []
    this.curidx = 0
    this.rounds = 0
    this.countDown = 60

    gamePlayers1.posIdx = 0
    gamePlayers2.posIdx = 1

    this.gamePlayers.push(gamePlayers1)
    this.gamePlayers.push(gamePlayers2)

    this.idGamePlayerMap[gamePlayers1.id] = gamePlayers1
    this.idGamePlayerMap[gamePlayers2.id] = gamePlayers2

    var playerCards1 = gamePlayers1.cardGroup;
    var playerCards2 = gamePlayers2.cardGroup;
    for(var i = 0; i < playerCards1.length; i++){
        var dbcard = DBTools.getDBCardById(playerCards1[i].dbid)
        var gamecard = new GameCard(playerCards1[i].id, dbcard)
        gamePlayers1.cards.push(gamecard)
    }

    for(var i = 0; i < playerCards2.length; i++){
        var dbcard = DBTools.getDBCardById(playerCards2[i].dbid)
        var gamecard = new GameCard(playerCards2[i].id, dbcard)
        gamePlayers2.cards.push(gamecard)
    }

    if(Math.random() < 0.5){
        this.curidx = 1;
    }

    this.room.broadcastAll(Defines.GameEvent_InitPlayer_End, this.ToData());
    
    this.DrawCards(gamePlayers1.id, 3);
    this.DrawCards(gamePlayers2.id, 3);

    this.room.broadcastAll(Defines.GameEvent_PreSummon_Start, {});
}

GameLogic.prototype.handlerEvent = function(event){

}

GameLogic.prototype.preSummon = function(msg){
    var playerid = msg.playerId
    var cardIds = msg.cardIds
    this.getPlayerById(playerid).preSummon = true

    var isPreSummonEnd = true
    for(var i = 0; i < 2; i++){
        if(this.gamePlayers[i].preSummon == false){
            isPreSummonEnd = false
        }
    }

    if(isPreSummonEnd == true){
        this.roundStart()
    }
}

GameLogic.prototype.roundStart = function(){
    var playerid = this.gamePlayers[this.curidx].id
    eventCenter.notify(GameEvent_OnTurn, {playerId:playerid})
    if(this.checkNeedDrawCard(playerid) == true){
        this.DrawCards(playerid, 1)
    }  
}

GameLogic.prototype.checkNeedDrawCard = function(playerid){
    var player = this.getPlayerById(playerid)
    var needDrawNum = 3 - player.activeCards.length + player.hardCards.length
    if(needDrawNum > 0){
        this.DrawCards(playerid, 1)
    }
}

GameLogic.prototype.roundEnd = function(){
    this.curidx = this.curidx + 1
    this.roundStart(this.curidx%2)
}

GameLogic.prototype.DrawCards = function(playerid, cardNum){
    var player = this.idGamePlayerMap[playerid]
    var drawCards = []
      
    if(player.cards.length == 0){
        console.log('玩家没有卡组')
        return
    }

    for(var i = 0; i < cardNum; i++){
        var drawidx = Math.floor(Math.random() * player.cards.length);
        player.hardCards.push(player.cards[drawidx])
        drawCards.push(player.cards[drawidx])
        player.cards.splice(drawidx, 1);  
    }

    var drawData = {
        player:player,
        cards:drawCards
    }

    this.room.broadcastAll(Defines.GameEvent_DrawCards, drawData)
}

GameLogic.prototype.SummonMonster = function(msg){
    var playeridx = msg.playeridx
    var gameCardId = msg.gameCardId
    var cardpos = msg.cardpos

    var player = this.gamePlayers[playeridx]
    var find = false
    var summonInfo = null
    var gameCard = null
    var posIllegal = false

    for(var i = 0; i < player.hardCards.length; i++){
        if(player.hardCards[i].id == gameCardId){
            gameCard = player.hardCards[i]
            for(var n = 0; n < player.activeCards.length; n++){
                if(player.activeCards[n].pos == cardpos){
                    posIllegal = true
                    break
                }
            }

            if(posIllegal == false){
                gameCard.pos = cardpos
                summonInfo = {
                    card:gameCard,
                    playeridx:playeridx
                }
                player.activeCards.push(gameCard)
                player.hardCards.splice(i, 1);
                find = true
            }
            break
        } 
    }

    if(find){
        eventCenter.notify(Defines.GameEvent_Summon_Monster, summonInfo)
    }
}

GameLogic.prototype.attack = function(msg){
    var gameCardAttack = msg.gameCardAttack
    var gameCardDef = msg.gameCardDef
    var attackskillid = msg.attackskillid
    var attackinfo = {}
    var damage = 0
    if(attackskillid == -1){
        damage = gameCardAttack.dbCard.attack[gameCardAttack.lv]
    }
    else{
        //check have skill
        if(gameCardAttack.dbCard.skillSp[gameCardAttack.lv] == attackskillid){
            
        }
    }
}

GameLogic.prototype.getPlayerById = function(id){
    return this.idGamePlayerMap[id]
}

GameLogic.prototype.ToData = function(){
    
    var outPlayers = []
    outPlayers[0] = this.getGamePlayerData(this.gamePlayers[0])
    outPlayers[1] = this.getGamePlayerData(this.gamePlayers[1])

    return {
        gamePlayers: outPlayers,
        curidx: this.curidx,
        rounds: this.rounds,
        countDown: this.countDown
    }
}
 
GameLogic.prototype.getGamePlayerData = function(gamePlayer){
    var data = {}
    data.id = gamePlayer.id
    data.hardCards = gamePlayer.hardCards;
    data.activeCards = gamePlayer.activeCards;
    data.sideCards = gamePlayer.sideCards;
    data.energyPoint = gamePlayer.energyPoint;
    data.hp = gamePlayer.hp;

    return data
}
